// app.js
var hosts = "https://testwangoula.artqiyi.com"; //测试
// var hosts = "https://api.wangola.com"; //生产
App({
  onLaunch: function() {
    this.getSystemInfo();
    this.newWorkListener();
  },
  //获取手机的相关参数
  getSystemInfo: function() {
    wx.getSystemInfo({
      success: (res) => {
        console.log(res)
        // this.globalData.SDKVersion = 1.1
        this.globalData.model = res.model;
        this.globalData.pixelRatio = 750 / res.windowWidth;
        console.log(`dpr:${this.globalData.pixelRatio}`)
        this.globalData.windowWidthpx = res.windowWidth;
        this.globalData.pageWidth = res.windowWidth;
        this.globalData.screenHeight = res.screenHeight * this.globalData.pixelRatio;
        if (res.model.indexOf('iPhone X') != -1) {
          this.globalData.windowHeight = (res.screenHeight - 88) * this.globalData.pixelRatio;
          this.globalData.mainpageHeight = (res.screenHeight - 88 - 82) * this.globalData.pixelRatio;
        } else {
          this.globalData.windowHeight = (res.screenHeight - 64) * this.globalData.pixelRatio;
          this.globalData.mainpageHeight = (res.screenHeight - 64 - 48) * this.globalData.pixelRatio;
        }
        this.globalData.pageHeight = this.globalData.windowHeight;
        this.globalData.pageHeightPx = this.globalData.windowHeight / this.globalData.pixelRatio;
        this.globalData.platform = res.platform.toLowerCase();
      }
    })
  },

  /**
   * 监听网络状态
   */
  newWorkListener: function() {
    wx.onNetworkStatusChange((res) => {
      this.globalData.isConnected = res.isConnected;
    })
  },

  Ajax: function(options) {
    var url = '';
    if (options.url) {
      if (options.url.indexOf('https') == -1) {
        url = hosts + options.url;
      } else {
        url = options.url;
      }
    }

    var header = {
      'X-App-Client': 'mini_program',
      'X-Product-Version': '2.1.0'
    }
    if (options.method == "POST") {
      header['content-type'] = 'application/x-www-form-urlencoded';
    }
    if (this.globalData.login_token) {
      header['Authorization'] = 'Bearer ' + this.globalData.login_token;
    } else if (wx.getStorageSync("login_token")) {
      this.globalData.login_token = wx.getStorageSync("login_token");
      header['Authorization'] = 'Bearer ' + wx.getStorageSync("login_token");
    }

    wx.request({
      url: url,
      data: options.data || {},
      method: options.method || 'GET',
      header: header,
      success: function(res) {
        // console.log(res)
        //未授权，返回登录页面
        if (res.statusCode == 401) {
          var pages = getCurrentPages();
          var currentPage = pages[pages.length - 1];
          var routeDepth = currentPage.route.split('/');
          var url = 'pages/login/login';
          for (var i = 0; i < routeDepth.length - 1; i++) {
            url = '../' + url;
          }
          wx.reLaunch({
            url: url,
          })
          return;
        }

        res = res.data;
        if (res.state) {
          if (res.state.code == 0) {
            if (options.success) {
              options.success(res.data);
            }
          } else {
            if (options.fail) {
              options.fail(res.state.msg, res.state.code);
            }
          }
        } else {
          if (options.fail) {
            options.fail('无返回数据');
          }
        }
      },
      fail: function(err) {
        if (options.fail) {
          options.fail('请求超时，请检查您的网络状况', -1);
        }
      }
    })
  },

  /**
   * 获取用户信息
   */
  getUserInfo: function(cb) {
    if (this.globalData.userInfo) {
      this.updateUserStars(() => {
        typeof cb == 'function' && cb(this.globalData.userInfo);
      })
      return;
    }

    this.Ajax({
      url: '/api/index.php?app=account&act=profile',
      data: {
        game_id: this.globalData.game_id
      },
      success: (res) => {
        this.globalData.userInfo = res;
        typeof cb == 'function' && cb(this.globalData.userInfo);
      }
    })
  },

  /**
   * 更新用户信息
   */
  updateUserInfo: function(cb) {
    this.Ajax({
      url: '/api/index.php?app=account&act=profile',
      success: (res) => {
        this.globalData.userInfo = res;
        typeof cb == 'function' && cb(this.globalData.userInfo);
      }
    })
  },

  /**
   * 更新用户星星
   */
  updateUserStars: function(cb) {
    if (this.globalData.userInfo) {
      this.Ajax({
        url: '/api/index.php?app=user&act=user_info',
        success: (res) => {
          this.globalData.userInfo.stars = res.stars;
          this.globalData.userInfo.total_stars = res.total_stars;
          typeof cb == 'function' && cb(this.globalData.userInfo);
        }
      })
    }
  },

  globalData: {
    game_id: 'wawa'
  }
})